package com.cn.yz.common.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;

public class EncryptUtil {
		
	private static final String KEY_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	
	private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

	/**
	 * 获取salt
	 * @param length
	 * @return
	 */
	public static String getSalt(int length) {
		//随机字符串的随机字符库
		StringBuffer sb = new StringBuffer();
		int len = KEY_STRING.length();
		for (int i = 0; i < length; i++) {
			sb.append(KEY_STRING.charAt((int) Math.round(Math.random() * (len - 1))));
		}
		return sb.toString();
	}
	
	/**
	 * md5加密
	 * @param targetStr
	 * @param salt
	 * @return
	 * @throws NoSuchAlgorithmException 
	 * @throws Exception
	 */
	public static String encryptMd5(String targetStr, String salt) throws NoSuchAlgorithmException{
		if(targetStr == null){
			return null;
		}
		StringBuffer str = new StringBuffer(targetStr); 
		str.append(salt);
		// 生成一个MD5加密计算摘要
		MessageDigest md = MessageDigest.getInstance("MD5");
		// 计算md5函数
		md.update(str.toString().getBytes());
		// digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
		// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
		return new BigInteger(1, md.digest()).toString(16);
    }
	 
	/**
	 * sha1加密
	 * @param targetStr
	 * @param salt
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static String encryptSha1(String targetStr, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		if (null == targetStr){
			return null;
		}
		StringBuffer str = new StringBuffer(targetStr); 
		str.append(salt);
		
		MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
		mdTemp.update(str.toString().getBytes("UTF-8"));
		
		//加密算法
		byte[] md = mdTemp.digest();
		int j = md.length;
		char[] buf = new char[j * 2];
		int k = 0;
		for (int i = 0; i < j; i++) {
			byte byte0 = md[i];
			buf[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
			buf[k++] = HEX_DIGITS[byte0 & 0xf];
		}
		return new String(buf);
		  
	}
	
	/**
	 * base64加密
	 * @param targetStr
	 * @return
	 * @throws Exception
	 */
	public static String encryptBase64(String targetStr){
		if (null == targetStr){
			return null;
		}
		byte[] input = targetStr.getBytes();
		return new String(Base64.encodeBase64(input));
	}
	
	/**
	 * base64解密
	 * @param targetStr
	 * @return
	 */
	public static String decryptBase64(String targetStr){
		if (null == targetStr){
			return null;
		}
		return new String(Base64.decodeBase64(targetStr));
	}
//	 
	 public static void main(String[] args) throws Exception{
		 String str1 = EncryptUtil.encryptMd5("password", null);
		 System.out.println(str1);
		 String str2 = EncryptUtil.encryptBase64(str1);
		 System.out.println(str2);
		 String str3 = EncryptUtil.decryptBase64("11");
		 System.out.println(str3);
		 String str4 = EncryptUtil.encryptSha1(str3, null);
		 System.out.println(str4);
	 }
}
